/*
 * @Description:
 * @Version: 2.0
 * @Autor: swc
 * @Date: 2024-01-23 11:04:08
 * @LastEditors: wiz
 * @LastEditTime: 2024-04-23 10:41:39
 */
import { ifs, Tsrv } from './config';

class T_cmd {
  // 主组件箱
  private compBox: ifs.I_compBox;

  // 主组件ID
  private compID: string;

  // 主组件实例
  private self: any;
  // 主组refs

  // 主组件created里初始化
  public init(self: any, compBoxInfo: ifs.I_compBoxInfo) {
    this.self = self;
    this.compBox = compBoxInfo.TcompBox;
    this.compID = compBoxInfo.compID;
  }

  public getTcmd(): T_cmd {
    return this;
  }

  public getFunColumnConf(funName: string, config: any) {
    const funInfo = Tsrv.getMainFunInfoByFunName(funName);
    if (Tsrv.utils.noValue(funInfo)) {
      Tsrv.reqGetFunInfo(funName).then(fInfo => {
        if (!Tsrv.utils.noValue(fInfo)) {
          this.self.$store.commit('SET_FUNINFO', {
            key: fInfo.mainFun,
            val: fInfo
          });
          Tsrv.getFunConf(funName).then(res => {
            if (res.isErr()) {
              console.error(res.errmsg);
            } else {
              this.setFieldsByFunConf(res.getReturnData(funName), config);
            }
          });
        }
      });
    } else {
      Tsrv.getFunConf(funName).then(res => {
        if (res.isErr()) {
          console.error(res.errmsg);
        } else {
          this.setFieldsByFunConf(res.getReturnData(funName), config);
        }
      });
    }
  }

  private setFieldsByFunConf(funConf: any, config: any) {
    if (Tsrv.utils.noValue(funConf?.tableColumns)) {
      return;
    }
    if (Tsrv.utils.noValue(config?.tableColumns)) {
      return;
    }
    config.tableColumns = Tsrv.utils.clone(funConf.tableColumns, true);
  }
}
interface I_cmd extends T_cmd {}
export { I_cmd, T_cmd };
